﻿using DotNetCore.CAP;

namespace WebAPI.Areas.Demo.Controllers
{
    /// <summary>
    /// 演示cap事件总线发布、订阅消息
    /// 可以访问 http://localhost:xxx/cap 这个地址打开Dashboard
    /// </summary>
    [ApiVersion("1.0")]
    public class CAPController : DemoBaseController
    {

        [HttpGet]
        [Route("send"), MapToApiVersion("1.0")]
        public IActionResult SendMessage([FromServices] ICapPublisher capBus)
        {
            var header = new Dictionary<string, string>()
            {
                ["my.header.first"] = "first",
                ["my.header.second"] = "second"
            };
            capBus.Publish("test.show.time", DateTime.Now, header);

            return Ok();
        }

        [NonAction]
        [CapSubscribe("test.show.time")]
        public void ReceiveMessage(DateTime time, [FromCap] CapHeader header)
        {
            Console.WriteLine("message time is:" + time);
            Console.WriteLine("message firset header :" + header["my.header.first"]);
            Console.WriteLine("message second header :" + header["my.header.second"]);
            throw new Exception("消息订阅异常");
        }

    }
}
